/*
 * 英才汇硕信息科技有限公司 拥有本软件版权 2021 并保留所有权利。
 * Copyright 2021, YCHS Information&Science Techology Co.,Ltd,
 * All right reserved.
 */
package com.ychs.lawyerback.service.impl;

import com.ychs.lawyerback.common.ResultCode;
import com.ychs.lawyerback.dto.ContractCancellationsQueryDTO;
import com.ychs.lawyerback.dto.ContractQueryDTO;
import com.ychs.lawyerback.exception.BussinessException;
import com.ychs.lawyerback.mapper.ContractCancellationsMapper;
import com.ychs.lawyerback.mapper.ContractMapper;
import com.ychs.lawyerback.pojo.ContractCancellations;
import com.ychs.lawyerback.pojo.ContractEntity;
import com.ychs.lawyerback.service.ContractService;
import com.ychs.lawyerback.vo.ContractCancellationsVo;
import com.ychs.lawyerback.vo.ContractQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mayujing
 * @version 1.0
 */
@Service
public class ContractServiceImpl implements ContractService {
    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private ContractCancellationsMapper contractCancellationsMapper;

    @Override
    public Map<String,Object> getContractsByCondition(ContractQueryDTO contractQueryDTO) {
        List<ContractQueryVo> contractList = contractMapper.getContractsByCondition(contractQueryDTO);

        int total = contractMapper.getCountByCondition(contractQueryDTO);
        Map<String, Object> result = new HashMap<>();
        result.put("list", contractList);
        result.put("total", total);
        return result;
    }

    @Override
    public int addContract(ContractEntity contractEntity) {
        return contractMapper.addContract(contractEntity);
    }

    @Override
    public int editContract(ContractEntity contractEntity) {
        return contractMapper.editContract(contractEntity);
    }

    @Override
    @Transactional
    public int delContract(ContractEntity contractEntity) {

        //添加注销表里
        ContractCancellations contractCancellations = new ContractCancellations();
        contractCancellations.setCancellationId(contractEntity.getContractId());
        contractCancellations.setContractNumber(contractEntity.getContractNumber());
        contractCancellations.setContractName(contractEntity.getContractName());
        contractCancellations.setContractType(contractEntity.getContractType());
        contractCancellations.setLawyerName(contractEntity.getLawyerName());
        contractCancellations.setCancellationReason(contractEntity.getCancellationReason());
        contractCancellations.setOperator(contractEntity.getOperator());
        contractCancellations.setRemarks(contractEntity.getRemarks());

        contractCancellations.setCancellationDate(new Date());

        int res = contractCancellationsMapper.addContractCancellation(contractCancellations);
        if (res > 0) {
            return contractMapper.changeContractActive(contractEntity);
        }else {
            throw new BussinessException(ResultCode.ADD_CONTRACT_FAIL);
        }
    }

    @Override
    public int changeContractActive(ContractEntity contractEntity) {
        return contractMapper.changeContractActive(contractEntity);
    }

    @Override
    public Map<String, Object> getCancelContract(ContractCancellationsQueryDTO contractCancellationsQueryDTO) {
        List<ContractCancellationsVo> canceledList = contractCancellationsMapper.getCancelContract(contractCancellationsQueryDTO);

        int total = contractCancellationsMapper.getCountCancelContract(contractCancellationsQueryDTO);

        Map<String, Object> result = new HashMap<>();
        result.put("list", canceledList);
        result.put("total", total);
        return result;
    }

    @Override
    public int reStart(String contractName) {
        int i = contractCancellationsMapper.delCancelContract(contractName);
        return contractCancellationsMapper.reStart(contractName);
    }


}
